﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.StopZero.CApStopZeroSpheresValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.StopZero.Resources;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.StopZero;

public sealed class CApStopZeroSpheresValidator : CApStopZeroAdditionalInfoCachingValidator
{
  private int _maxSpheres = 6;
  private readonly Func<List<ITxLocatableObject>> _getAttachementTools;
  private readonly Func<List<ITxLocatableObject>> _getMountedTools;
  private readonly Func<ITxRobot> _getRobot;

  public CApStopZeroSpheresValidator(
    Func<List<ITxLocatableObject>> getAttachementTools,
    Func<List<ITxLocatableObject>> getMountedTools,
    Func<ITxRobot> getRobot)
    : base(StopZeroStringTable.NoSpheresError)
  {
    this._getAttachementTools = getAttachementTools;
    this._getMountedTools = getMountedTools;
    this._getRobot = getRobot;
  }

  public List<ITxObject> GetScopeSpheres(ITxObjectCollection spheresContainer)
  {
    return !(this[(ITxObject) spheresContainer] is List<ITxObject> collection) ? new List<ITxObject>() : new List<ITxObject>((IEnumerable<ITxObject>) collection);
  }

  protected override string GetCachedValidationError(ITxObject obj)
  {
    List<ITxObject> sphereChildren = CApStopZeroSpheresValidator.GetSphereChildren(obj);
    List<ITxLocatableObject> source = this._getMountedTools();
    ITxRobot itxRobot = this._getRobot();
    if (sphereChildren.Count == 0)
      return StopZeroStringTable.NoSpheresError;
    if (itxRobot.Controller.Name.Contains("Kuka"))
      this._maxSpheres = !TxFeatureManager.CheckFeature("KUKA_STOPZERO_UNLIMITEDSPHERES") ? 12 : -1;
    if (this._maxSpheres > 0 && sphereChildren.Count > this._maxSpheres)
      return string.Format(StopZeroStringTable.TooMuchSpheresError, (object) this._maxSpheres);
    if (obj is ITxFolder)
    {
      if (!this.IsSafityFolder(obj as ITxFolder))
        return StopZeroStringTable.NotSafetyFolder;
      if (!this.SafityFolderHasAttachedTool(obj as ITxFolder))
        return StopZeroStringTable.NotAttachedToToolError;
    }
    else if (obj is TxSafetyObject)
    {
      if (!((IEnumerable<ITxObject>) source).Contains<ITxObject>(obj))
        return StopZeroStringTable.NotAttachedToToolError;
    }
    else if (!this.IsUnderAttachementParent(obj))
      return StopZeroStringTable.NotAttachedToToolError;
    this[obj] = (object) sphereChildren;
    return (string) null;
  }

  private static List<ITxObject> GetSphereChildren(ITxObject obj)
  {
    if (!(obj is ITxObjectCollection objectCollection))
      return new List<ITxObject>();
    TxTypeFilter txTypeFilter = new TxTypeFilter(new Type[2]
    {
      typeof (ITxComponent),
      typeof (ITx2Or3DimensionalGeometry)
    });
    HashSet<ITxObject> allSpheres = new HashSet<ITxObject>(((IEnumerable<ITxObject>) objectCollection.GetAllDescendants((ITxTypeFilter) txTypeFilter)).Where<ITxObject>((Func<ITxObject, bool>) (geometry => CApStopZeroSpheresValidator.IsShpere(geometry))));
    return allSpheres.Where<ITxObject>((Func<ITxObject, bool>) (sphere => !allSpheres.Contains((ITxObject) sphere.Collection))).ToList<ITxObject>().Where<ITxObject>((Func<ITxObject, bool>) (x => (x as ITxDisplayableObject).Visibility == null || (x as ITxDisplayableObject).Visibility == 1)).ToList<ITxObject>();
  }

  private static bool IsShpere(ITxObject obj)
  {
    try
    {
      TxVector txVector;
      double num;
      TxGraphicsUtilitiesEx.GetSphereParameters(obj, ref txVector, ref num);
      return true;
    }
    catch (TxInvalidObjectException ex)
    {
      return false;
    }
  }

  private bool IsSafityFolder(ITxFolder obj)
  {
    if (!(obj is ITxMainFolder))
    {
      TxObjectList directDescendants = ((ITxObjectCollection) obj).GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxSafetyObject)));
      if (directDescendants != null && ((Collection<ITxObject>) directDescendants).Count > 0)
        return true;
    }
    return false;
  }

  private bool SafityFolderHasAttachedTool(ITxFolder obj)
  {
    List<ITxLocatableObject> source = this._getMountedTools();
    foreach (ITxObject directDescendant in (Collection<ITxObject>) ((ITxObjectCollection) obj).GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxSafetyObject))))
    {
      if (((IEnumerable<ITxObject>) source).Contains<ITxObject>(directDescendant))
        return true;
    }
    return false;
  }

  private bool IsUnderAttachementParent(ITxObject obj)
  {
    List<ITxLocatableObject> itxLocatableObjectList = this._getAttachementTools();
    bool flag = false;
    foreach (ITxObject itxObject in itxLocatableObjectList)
    {
      flag = !TxAttachmentUtilsEx.IsAttachmentToParentPossible(itxObject, obj, (TxAttachmentType) 0);
      if (flag)
        break;
    }
    return flag;
  }
}
